if(Ext.isIE){
   Ext.enableGarbageCollector=false;
}
Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath('Ext.ux', 'scripts/ext/ux/');

Ext.require([
  'Ext.tab.*',
  'Ext.ux.extend.*',
  'Ext.ux.extend.MenuSearchField',
  'Ext.ux.extend.SelectField',
  'Ext.ux.extend.GridSearch', 
  'Ext.ux.extend.ExcelExport',
  'Ext.ux.extend.TreeGridFilter',
  'Ext.ux.TabCloseMenu',
  'Ext.ux.TreePicker',
  'Ext.ux.extend.TreePathPicker',
  'Ext.ux.statusbar.StatusBar',
  'Ext.ux.extend.FileDownload',
  'Ext.ux.grid.FiltersFeature',
  'Ext.ux.ProgressBarPager',
  'Ext.ux.form.MultiSelect',
  'Ext.ux.form.ItemSelector',
  'Ext.tip.QuickTipManager',
//  'Ext.ux.ajax.JsonSimlet',
//  'Ext.ux.ajax.SimManager',
  'Ext.grid.plugin.BufferedRenderer'
]);

var pageStore = Ext.create('Ext.data.Store', {
	fields: ['abbr', 'name'],
    data: [
	    { "abbr": 15, "name": 15 },
	    { "abbr": 20, "name": 20 },
	    { "abbr": 30, "name": 30 },
		{ "abbr": 50, "name": 50 },
		{ "abbr": 100, "name": 100 },
		{ "abbr": 200, "name": 200 }
    ]
});

Ext.define('TreeModel', {
    extend: 'Ext.data.Model',
    fields: [{ name: "id"},{ name: "orderid"},{ name: "parentid"},{ name: "text"},{ name: "qtip"},
            { name: "url"},{ name: "iconCls"},{ name: "leaf"},{ name: "showflag"},{name:"params"}]
});

Ext.define('menuTree', {
	mixins: {observable: 'Ext.util.Observable'},
	constructor: function (config) {
		this.mixins.observable.constructor.call(this, config);
	},
	store: function (Nodeid) {
		var store = Ext.create('Ext.data.TreeStore', {
			model: TreeModel,
			proxy: {
				type: 'ajax',
				url: 'getMenus.action',
				reader: {type: 'json'}
			},
			root: {id: Nodeid,expanded: false}
		});
		return store;
	},
	getMenu: function () {
		var treeItems = this.initMenuData();
		var menu = Ext.create('Ext.Panel', {
			id: "menubar",
			region: 'west',
			border: false,
			collapsible: true,
			titleCollapse: true,
			split: true,
			border: false,
			width: 200,
			iconCls: 'navimenu',
			title: '功能菜单',
			hideCollapseTool: false,
			layout: 'accordion',
//			dockedItems: [{
//				id:'menuSearcher',
//				dock:'top',
//				fieldLabel: '搜索',
//				width: 150,
//				labelWidth: 50,
//				xtype: 'menusearchfield',
//				store:  Ext.create('Ext.data.TreeStore', {
//					model: TreeModel
//				})
//			}],
			items: treeItems
		});
		return menu;
	},
	initMenuData: function () {
		var menuDataArr = new Array();
        Ext.Ajax.request({
            url: 'getMenus.action',
            params: {node: 0},
            async: false,
            scope: this,
            success: function (response) {
                var result = Ext.JSON.decode(response.responseText);
                if (Ext.isDefined(result)) {
                    var i = 0;
                    var menus = result;
                    Ext.each(menus, function (data) {
                        var tree = Ext.create('Ext.tree.Panel', {
                            id: data.id, 
                            el: data.id, 
                            title: data.text,
                            iconCls: data.iconCls,
                            useArrows: true,
                            rootVisible: false,
                            lines: false,
                            autoScroll: true,
                            collapsible: true,
                            store: this.store(data.id),
                            listeners: {
                                'itemclick': function (view, record, item, index, e) {
                                    if (record.raw.leaf) {
                                        var center = Ext.getCmp('mainContent');
                                        var ntb = Ext.getCmp(record.raw.id);
                                        if (ntb == undefined) {
                                            var tb = new Ext.Panel({
                                                id: record.raw.id,
                                                title: record.raw.text,
                                                tabTip: record.raw.text,
                                                closable: true,
                                                closeAction: 'destroy',
                                                iconCls: record.raw.iconCls,
                                                cardSwitchAnimation: 'slide',
                                                layout: 'anchor',
                                                autoScroll:true,
                                                autoLoad: { url: record.raw.url, params: { params: record.raw.param }, scripts: true },
                                                waitMsgTarget: true
                                            });
                                            center.add(tb);
                                            center.setActiveTab(tb);
                                        }
                                        else
                                            center.setActiveTab(ntb);
                                    }
                                }
                            }
                        });
                        
                        menuDataArr[i] = tree;
                        i++;
                    }, this);
                }
            }
        });
        return menuDataArr;
    }
});
	
Ext.onReady(function() {
	Ext.QuickTips.init();
	if ( username == null) return;

	var south_item = new BuildBottomPanel().BottomPanel;
	var center_item = new Ext.createWidget('tabpanel', {
		id:'mainContent',
        region:'center',
        resizeTabs: true,
        enableTabScroll: true,
        width: 600,
        height: 250,
        layout:'fit',
        defaults: {
	        autoScroll:true,
	        bodyPadding: 0
        },
        plugins: Ext.create('Ext.ux.TabCloseMenu', {
            extraItemsTail: [
                '-',
                {
                    text: '是否可关闭',
                    checked: true,
                    hideOnClick: true,
                    handler: function (item) {
                        currentItem.tab.setClosable(item.checked);
                    }
                }
            ],
            listeners: {
                aftermenu: function () {
                    currentItem = null;
                },
                beforemenu: function (menu, item) {
                    var menuitem = menu.child('*[text="是否可关闭"]');
                    currentItem = item;
                    menuitem.setChecked(item.closable);
                }
            }
        })
        })
    var mainTree = Ext.create('menuTree');
	new Ext.Viewport( {
		layout : 'border',  
		items : [
			Ext.create('Ext.Component', {
				id:'header',
				title :  userCname + '，欢迎您!',
				collapsible : true,
				html:'<table style="width:100%;background:url(styles/images/bg.png)"><tr><td style="height:100%;background-image:url(styles/images/banner.png);background-repeat:repeat-x;font-weight:bold;font-size:36px;"><font color=white>AliMigration</font></td></tr></table>',
				region : 'north',
//				height : 72
				height : 50
			}),mainTree.getMenu(),center_item,south_item
		]
	});
});